//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LmLinearExponentialVolatilityModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
#include <gen/QL/Legacy/Libormarketmodels/LmVolatilityModel.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (Cephei::IVector<Double>^ fixingTimes, Double a, Double b, Double c, Double d) : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
    CoVector<Double>^ _CfixingTimes;
    try
    {
#ifdef HANDLE
        _phLmLinearExponentialVolatilityModel = NULL;
#endif
        CoVector<Double>^ _CfixingTimes = safe_cast<CoVector<Double>^> (fixingTimes);
        _CfixingTimes->Lock();
        INativeVector<Double>^ _NCIfixingTimes = _CfixingTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixingTimes = safe_cast<CDoubleVector^>(_NCIfixingTimes);
        std::vector<QuantLib::Time>& _fixingTimes = static_cast<std::vector<QuantLib::Time>&> (_NCfixingTimes->GetReference ());
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a);
        QuantLib::Real _b = (QuantLib::Real)ValueHelper::Convert (b);
        QuantLib::Real _c = (QuantLib::Real)ValueHelper::Convert (c);
        QuantLib::Real _d = (QuantLib::Real)ValueHelper::Convert (d);
        _ppLmLinearExponentialVolatilityModel = new boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel> (new QuantLib::LmLinearExponentialVolatilityModel ( _fixingTimes,  _a,  _b,  _c,  _d ));
        SetLmVolatilityModel (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfixingTimes != nullptr) _CfixingTimes->Unlock();
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel>& childNative, Object^ owner) : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialVolatilityModel = NULL;
#endif
	_ppLmLinearExponentialVolatilityModel = &childNative;
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (QuantLib::LmLinearExponentialVolatilityModel& childNative, Object^ owner) : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialVolatilityModel = NULL;
#endif
	_ppLmLinearExponentialVolatilityModel = new boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel> (&childNative);
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
    _LmLinearExponentialVolatilityModelOwner = owner;
    _LmVolatilityModelOwner = owner;
}

Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (CLmLinearExponentialVolatilityModel^ copy) : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialVolatilityModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmLinearExponentialVolatilityModel = new boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel> (copy->GetShared());
        _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (System::Type^ t) : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialVolatilityModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmLinearExponentialVolatilityModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (QuantLib::Handle<QuantLib::LmLinearExponentialVolatilityModel>& childNative, Object^ owner)  : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
	_phLmLinearExponentialVolatilityModel = &childNative;
	_ppLmLinearExponentialVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel>>(childNative.currentLink());
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
    _LmLinearExponentialVolatilityModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (QuantLib::Handle<QuantLib::LmLinearExponentialVolatilityModel> childNative)  : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
	_phLmLinearExponentialVolatilityModel = &childNative;
	_ppLmLinearExponentialVolatilityModel = &static_cast<boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel>>(childNative.currentLink());
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::CLmLinearExponentialVolatilityModel (QuantLib::LmLinearExponentialVolatilityModel childNative)  : CLmVolatilityModel(CLmLinearExponentialVolatilityModel::typeid)
{
#ifdef HANDLE
	_phLmLinearExponentialVolatilityModel = NULL;
#endif
	_ppLmLinearExponentialVolatilityModel = new boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel> (new QuantLib::LmLinearExponentialVolatilityModel (childNative));
    _ppLmVolatilityModel = new boost::shared_ptr<QuantLib::LmVolatilityModel> (boost::dynamic_pointer_cast<QuantLib::LmVolatilityModel> (*_ppLmLinearExponentialVolatilityModel));
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::~CLmLinearExponentialVolatilityModel ()
{
    if (_ppLmLinearExponentialVolatilityModel != NULL)
    {
	    delete _ppLmLinearExponentialVolatilityModel;
        _ppLmLinearExponentialVolatilityModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::!CLmLinearExponentialVolatilityModel ()
{
    if (_ppLmLinearExponentialVolatilityModel != NULL)
    {
	    delete _ppLmLinearExponentialVolatilityModel;
    }
}
QuantLib::LmLinearExponentialVolatilityModel& Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::GetReference ()
{
    if (_ppLmLinearExponentialVolatilityModel == NULL) throw gcnew NativeNullException ();
	return **_ppLmLinearExponentialVolatilityModel;
}
boost::shared_ptr<QuantLib::LmLinearExponentialVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::GetShared ()
{
    if (_ppLmLinearExponentialVolatilityModel == NULL) throw gcnew NativeNullException ();
	return *_ppLmLinearExponentialVolatilityModel;
}
QuantLib::LmLinearExponentialVolatilityModel* Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::GetPointer ()
{
    if (_ppLmLinearExponentialVolatilityModel == NULL) throw gcnew NativeNullException ();
	return &**_ppLmLinearExponentialVolatilityModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmLinearExponentialVolatilityModel>& Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::GetHandle ()
{
	if (_phLmLinearExponentialVolatilityModel == NULL)
	{
		_phLmLinearExponentialVolatilityModel = new Handle<QuantLib::LmLinearExponentialVolatilityModel> (*_ppLmLinearExponentialVolatilityModel);
	}
	return *_phLmLinearExponentialVolatilityModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::HasNative () 
{
	return (_ppLmLinearExponentialVolatilityModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::IntegratedVariance (UInt64 i, UInt64 j, Double u, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j);
        QuantLib::Time _u = (QuantLib::Time)ValueHelper::Convert (u);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmLinearExponentialVolatilityModel)->integratedVariance ( _i,  _j,  _u,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Double Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel::Volatility (UInt64 i, Double t, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppLmLinearExponentialVolatilityModel)->volatility ( _i,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Legacy::Libormarketmodels::ILmLinearExponentialVolatilityModel^ Cephei::QL::Legacy::Libormarketmodels::CLmLinearExponentialVolatilityModel_Factory::Create (Cephei::IVector<Double>^ fixingTimes, Double a, Double b, Double c, Double d)
{
    return gcnew CLmLinearExponentialVolatilityModel ( fixingTimes,  a,  b,  c,  d);
}
